<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus';
import axios from 'axios';
import { useRouter } from 'vue-router';
const router = useRouter();
const form = ref({
  username: '',
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})
const handleSubmit = async () => {
  // 前端验证：两个新密码是否一致
  if (form.value.newPassword !== form.value.confirmPassword) {
    ElMessage.error('新密码和确认密码不一致');
    return;
  }

  // 发送请求到后端
  try {
    const response = await axios.put('/api/users/change', {
      username: form.value.username,
      oldPassword: form.value.oldPassword,
      newPassword: form.value.newPassword
    });

    if (response.data.success) {
      ElMessage.success('密码修改成功,即将跳转到登录界面...');
      // 跳转到登录页面或其他页面
      setTimeout(() => {
        router.push('/menu');
      }, 3000);
    } else {
      ElMessage.error(response.data.message || '用户名或密码不存在');
    }
  } catch (error) {
    ElMessage.error('服务器错误');
    console.error(error);
  }
};

const goBack = () => {
  // 返回登录页面的逻辑
  // 例如：router.push('/login');
  window.history.back();
};


</script>

<template>
  <div class="login-page">
    <div class="overlay">
      <div class="login-box">
        <h2 class="login-box h2">修改密码</h2>
        <div class="login-content">
          <!-- 左侧：第三方登录 -->
          <div class="third-party">
            <div class="form-left">
              <el-input v-model="form.username" placeholder="用户名" />
              <el-input v-model="form.oldPassword" type="password" placeholder="旧密码" />
            </div>
          </div>

          <!-- 右侧：账号密码登录 -->
          <div class="account-form">
            <el-input v-model="form.newPassword" type="password" placeholder="新密码" />
            <el-input v-model="form.confirmPassword" type="password" placeholder="确认新密码" />
          </div>
        </div>
        <div class="login-footer">
          <span class="span_left" @click="handleSubmit">修改密码</span>
          <span class="span_right" @click="goBack">返回登陆</span>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* ✅ 背景全屏铺满 */
.login-page {
  position: fixed;
  inset: 0;
  background: url('@/assets/bg.png') no-repeat center center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* ✅ 登录内容半透明白底、圆角 */
.overlay {
  background-color: rgba(255, 255, 255, 0.95);
  padding: 30px;
  border-radius: 10px;
  width: 90%;
  max-width: 800px;
}

.login-box h2 {
  color: black;
  text-align: center;
  margin-bottom: 20px;
}

.login-content {
  display: flex;
  gap: 30px;
  flex-direction: row;
  justify-content: space-between;
}

.third-party-btn {
  width: 90%;
  height: 40px;
  font-size: 14px;
  padding: 0;
}

.third-party,
.account-form {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.login-footer {
  margin-top: 20px;
  background: #555;
  color: white;
  display: flex;
  justify-content: space-between;
  padding: 10px;
  font-size: 14px;

}

.login-btn {
  width: 100%;
}

.span_left {
  width: 50%;
  display: flex;
  justify-content: center;   /* 水平居中 */
  align-items: center;       /* 垂直居中 */
}

.span_right {
  width: 50%;
  display: flex;
  justify-content: center;   /* 水平居中 */
  align-items: center;       /* 垂直居中 */

}
/* ✅ 响应式布局：小屏一列排布 */
@media (max-width: 800px) {
  .login-content {
    flex-direction: column;
  }

  .login-footer {
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }
}

.span_ui {
  margin-right: 50px;
}
</style>
<script setup>
</script>